﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
#if NETFX_CORE || WINDOWS_PHONE
using File = Windows.Storage.StorageFile;

#else
using File = System.IO.FileInfo;
#endif

namespace WindowsCommonStorage
{
    public abstract class CommonStorageFile:CommonStorageItem
    {


        public File EnvironmentFile 
        {
            get 
            {
                return ErrorHandler.Func(() => { return ProtectedEnvironmentFile; });
            }
        
        }
        internal protected abstract File ProtectedEnvironmentFile { get; }






        /// <summary>
        /// 指定されたファイルを現在のファイルに置換します
        /// </summary>
        /// <param name="fileToReplace">置き換えるファイル</param>
        /// <returns></returns>
        public async Task CopyAndReplaceAsync(CommonStorageFile fileToReplace)
        {
            await ErrorHandler.AsyncAction(async () =>
            {
                await ProtectedCopyAndReplaceAsync(fileToReplace).ConfigureAwait(false);

            }).ConfigureAwait(false);
        }
        internal protected abstract Task ProtectedCopyAndReplaceAsync(CommonStorageFile fileToReplace);


        public async Task<Stream> OpenStreamAsync()
        {
            return await ErrorHandler.AsyncFunc(async() =>
            {
                return await ProtectedOpenStreamAsync().ConfigureAwait(false);
            }).ConfigureAwait(false);
        }

        internal protected abstract Task<Stream> ProtectedOpenStreamAsync();



        public static async Task<CommonStorageFile> GetFileFromPathAsync(string path)
        {
#if !(NETFX_CORE || WINDOWS_PHONE)
            return await NetFxStorageFile.ProtectedGetFileFromPathAsync(path).ConfigureAwait(false);
#else
            return await NetFxCoreStorageFile.ProtectedGetFileFromPathAsync(path).ConfigureAwait(false);
#endif
        }


    }
}
